package com.black.knight.chess.model;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.black.knight.chess.domain.Settings;

public class SettingsModel {
	
	private static SettingsModel model;
	
	private boolean autoRotate;
	private boolean markFields;
	private boolean playOnClock;
	private boolean markLastMove;
	private boolean animateMove;
	private String playerName;
	private Integer gameDuration;
	private boolean showEvaluation;
	private boolean recognizeGame;
	private boolean recommendMove;
	private boolean playSound;
	private String language;
	private Integer tableSize;
	
	private String username;
	private String password;
	private boolean autoLogIn;
	private boolean autoLogInWithFacebook;

	//private User loggedUser;
	
	private String facebookToken;
	
	private Long facebookTokenExpiration;
	
	//private ActivityEnum lastActivity = ActivityEnum.UNKNOWN_ACTIVITY;
	
	public static String SERVICE_URL = "http://www.bk-chess.com";
	
	public static ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool();
	
	private SharedPreferences prefs;
	
	public SettingsModel() {
		this.prefs = PreferenceManager.getDefaultSharedPreferences(SahModel.context);
		
		this.autoRotate = prefs.getBoolean(Settings.AUTO_ROTATE, false);
		this.markFields = prefs.getBoolean(Settings.MARK_FIELDS, true);
		this.playOnClock = prefs.getBoolean(Settings.PLAY_ON_CLOCK, true);
		this.markLastMove = prefs.getBoolean(Settings.MARK_LAST_MOVE, true);
		this.animateMove = prefs.getBoolean(Settings.ANIMATE_MOVE, true);
		this.showEvaluation = prefs.getBoolean(Settings.SHOW_EVALUATION, true);
		this.recognizeGame = prefs.getBoolean(Settings.RECOGNIZE_GAME, true);
		this.recommendMove = prefs.getBoolean(Settings.RECOMMEND_MOVE, false);
		this.playerName = prefs.getString(Settings.PLAYER_NAME, "");
		this.gameDuration = prefs.getInt(Settings.GAME_DURATION, 15);
		this.language = prefs.getString(Settings.LANGUAGE, "ee");
		this.playSound = prefs.getBoolean(Settings.PLAY_SOUND, true);
		this.tableSize = prefs.getInt(Settings.TABLE_SIZE, 10);
		this.username = prefs.getString(Settings.USERNAME, "");
		this.password = prefs.getString(Settings.PASSWORD, "");
		this.autoLogIn = prefs.getBoolean(Settings.AUTO_LOG_IN, false);
		this.autoLogInWithFacebook = prefs.getBoolean(Settings.AUTO_LOG_IN_FACEBOOK, false);
		this.facebookToken = prefs.getString(Settings.FACEBOOK_TOKEN, "");
		this.facebookTokenExpiration = prefs.getLong(Settings.FACEBOOK_TOKEN_EXPIRATION, 0L);
	}
	
	public static SettingsModel getInstance() {
		if(model == null) {
			model = new SettingsModel();
		}
		return model;
	}
	
	public boolean isAutoRotate() {
		return autoRotate;
	}
	
	public boolean isMarkLastMove() {
		return markLastMove;
	}

	public boolean isAnimateMove() {
		return animateMove;
	}
	
	public boolean isRecommendMove() {
		return recommendMove;
	}
	
	public boolean isRecognizeGame() {
		return recognizeGame;
	}

	public boolean isShowEvaluation() {
		return showEvaluation;
	}
	
	public Integer getGameDuration() {
		return gameDuration;
	}
	
	public Integer getTableSize() {
		return tableSize;
	}
	
	public String getPlayerName() {
		if(username != null && username.length() > 0) return username;
		return playerName;
	}
	
	public  boolean isPlayOnClock() {
		return playOnClock;
	}
	
	public boolean isMarkFields() {
		return markFields;
	}
	
	public String getLanguage() {
		return language;
	}
	
	public boolean isPlaySound() {
		return playSound;
	}
	
	public void setAutoRotate(boolean autoRotate) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(Settings.AUTO_ROTATE, autoRotate);
		editor.commit();
		
		this.autoRotate = autoRotate;
	}
	
	public void setMarkLastMove(boolean markLastMove) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(Settings.MARK_LAST_MOVE, markLastMove);
		editor.commit();
		
		this.markLastMove = markLastMove;
	}

	public void setAnimateMove(boolean animateMove) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(Settings.ANIMATE_MOVE, animateMove);
		editor.commit();
		
		this.animateMove = animateMove;
	}

	public void setMarkFields(boolean markFields) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(Settings.MARK_FIELDS, markFields);
		editor.commit();
		
		this.markFields = markFields;
	}
	
	public void setPlayOnClock(boolean playOnClock) {		
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(Settings.PLAY_ON_CLOCK, playOnClock);
		editor.commit();
		
		if(!playOnClock) {
			SahModel.getInstance().getWhiteClock().stop();
			SahModel.getInstance().getBlackClock().stop();
		}
		
		this.playOnClock = playOnClock;
	}

	public void setPlayerName(String playerName) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(Settings.PLAYER_NAME, playerName);
		editor.commit();
		
		this.playerName = playerName;
	}

	public void setGameDuration(Integer gameDuration) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putInt(Settings.GAME_DURATION, gameDuration);
		editor.commit();
		
		this.gameDuration = gameDuration;
	}
	
	public void setTableSize(Integer tableSize) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putInt(Settings.TABLE_SIZE, tableSize);
		editor.commit();
		
		this.tableSize = tableSize;
	}

	public void setShowEvaluation(boolean showEvaluation) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(Settings.SHOW_EVALUATION, showEvaluation);
		editor.commit();
		
		this.showEvaluation = showEvaluation;
	}

	public void setRecognizeGame(boolean recognizeGame) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(Settings.RECOGNIZE_GAME, recognizeGame);
		editor.commit();
		
		this.recognizeGame = recognizeGame;
	}

	public void setRecommendMove(boolean recommendMove) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(Settings.RECOMMEND_MOVE, recommendMove);
		editor.commit();
		
		this.recommendMove = recommendMove;
	}

	public void setLanguage(String language) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(Settings.LANGUAGE, language);
		editor.commit();
		
		this.language = language;
	}

	public void setPlaySound(boolean playSound) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(Settings.PLAY_SOUND, playSound);
		editor.commit();
		
		this.playSound = playSound;
	}

	/*public User getLoggedUser() {
		return loggedUser;
	}

	public void setLoggedUser(User loggedUser) {
		this.loggedUser = loggedUser;
	}*/

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(Settings.USERNAME, username);
		editor.commit();
		
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(Settings.PASSWORD, password);
		editor.commit();
		
		this.password = password;
	}

	public boolean isAutoLogIn() {
		return autoLogIn;
	}

	public void setAutoLogIn(boolean autoLogIn) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(Settings.AUTO_LOG_IN, autoLogIn);
		editor.commit();
		
		this.autoLogIn = autoLogIn;
	}

	/*public ActivityEnum getLastActivity() {
		return lastActivity;
	}

	public void setLastActivity(ActivityEnum lastActivity) {
		this.lastActivity = lastActivity;
	}*/

	public String getFacebookToken() {
		return facebookToken;
	}

	public void setFacebookToken(String facebookToken) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(Settings.FACEBOOK_TOKEN, facebookToken);
		editor.commit();
		
		this.facebookToken = facebookToken;
	}

	public Long getFacebookTokenExpiration() {
		return facebookTokenExpiration;
	}

	public void setFacebookTokenExpiration(Long facebookTokenExpiration) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putLong(Settings.FACEBOOK_TOKEN_EXPIRATION, facebookTokenExpiration);
		editor.commit();
		
		this.facebookTokenExpiration = facebookTokenExpiration;
	}
	
	public boolean isAutoLogInWithFacebook() {
		return autoLogInWithFacebook;
	}

	public void setAutoLogInWithFacebook(boolean autoLogInWithFacebook) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(Settings.AUTO_LOG_IN_FACEBOOK, autoLogInWithFacebook);
		editor.commit();
		
		this.autoLogInWithFacebook = autoLogInWithFacebook;
	}
}
